package com.bourse.core.ibatis;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bourse.core.utils.Cotation;
import com.ibatis.sqlmap.client.SqlMapClient;

public class CotationIbatisDao implements CotationDao {

	/** The logger */
	private Logger LOGGER = Logger.getLogger(CotationIbatisDao.class);
	
	/** The SqlMapClient */
	private SqlMapClient sqlMapClient;
	
	
	public void insertCotation(List<Cotation> cotations){
		
		for(Cotation cotation : cotations){
		
			Map<String,Object> param = new HashMap<String, Object>(7);
			param.put("date", cotation.getDate());
			param.put("isin",cotation.getIsin());
			param.put("volume", cotation.getVolume());
			param.put("cours", cotation.getCours());
			param.put("ouverture", cotation.getOuverture());
			param.put("pbas", cotation.getPlusBas());
			param.put("phaut", cotation.getPlusHaut());			
			
			try{
				sqlMapClient.queryForList("INSERT_COTATION", param);
			}catch(SQLException e){
				LOGGER.error("[Cotation Integration] - Problem while insrting data: "+e.getMessage());
			}
			
		}
		
		
		
	}
	
	
	/**
	 * @see CotationDao#getCotations()
	 */
	@SuppressWarnings("unchecked")
	public List<Cotation> getCotations(){
		LOGGER.info("[Cotation loader] - Quering cotations");
		List<Cotation> cotations = new ArrayList<Cotation>();
		try{
			cotations = sqlMapClient.queryForList("GET_COTATION");
		}catch(SQLException e){
			LOGGER.error(e.getMessage());
		}
		return cotations;
	}
	
	
	/**
	 * Set the sqlMapClient
	 * @param sqlMapClient
	 */
	public void setSqlMapClient(SqlMapClient sqlMapClient){
		this.sqlMapClient = sqlMapClient;
	}
	
	
}
